﻿// *********************************************************************
// [DCOM Productions]
// [Copyright (C) DCOM Productions All rights reserved.]
// *********************************************************************

namespace BitFlex {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;

    /// <summary>
    /// Enables the deriving class to enable the application to use BitFlex command line pattern
    /// </summary>
    /// <example>
    /// Pattern:
    ///     -argument:value
    ///     -argument
    /// Example:
    ///     -path:"C:\Program Files\MyProgram"
    ///     -username:danderson
    ///     -password:bitflex
    ///     -enablevisualstyles
    ///     -directxmode
    /// </example>
    public abstract class CommandLineApplication {
        /// <summary>
        /// Initializes a new instance of the BitFlex.CommandLineApplication class
        /// </summary>
        public CommandLineApplication() {
            foreach (string item in Environment.GetCommandLineArgs()) {
                CommandLineArgument argument = new CommandLineArgument();
                if (Regex.Match(item, "-[a-zA-Z]*").Success) {
                    argument.Name = Regex.Match(item, "-[a-zA-Z]*").Value.Remove(0, 1);
                    if (Regex.Match(item, "-[a-zA-Z]*:").Success) {
                        argument.Value = item.Remove(0, item.IndexOf(':') + 1);
                    }
                }
                if (!string.IsNullOrEmpty(argument.Name)) {
                    Arguments.Add(argument);
                }
            }
        }

        private List<CommandLineArgument> m_Arguments = null;
        /// <summary>
        /// Gets or sets the list of command line arguments received by the application
        /// </summary>
        public List<CommandLineArgument> Arguments {
            get {
                if (m_Arguments == null) {
                    m_Arguments = new List<CommandLineArgument>();
                }
                return m_Arguments;
            }
            set {
                m_Arguments = value;
            }
        }

        /// <summary>
        /// Implemented by the deriving class to execute the command line arguments
        /// </summary>
        protected virtual void ExecuteArguments() {
        }
    }
}